package eu.jk.base.io;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;

/**
 * 一个文件操作类,面对文本文件
 * 
 * @author wenjianwzz
 * @version 0.5
 * @since wzz.eu 0.5
 */
public class Texter {

	String path;
	File file;
	RandomAccessFile randomFile;
	String fileEncoding = "UTF-8";

	/**
	 * @param path
	 *            文件路径
	 * @param createIfNotExist
	 *            当文件不存在的时候，是否建立新文件
	 */
	public Texter(String path, boolean createIfNotExist) {
		this.path = path;
		file = new File(path);
		if (createIfNotExist)
			if (!file.exists()) {
				try {
					file.createNewFile();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		try {
			randomFile = new RandomAccessFile(file, "rw");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 相当于TextOperator(path, false)
	 * @param path 文件路径
	 * @throws FileNotFoundException
	 */
	public Texter(String path) throws FileNotFoundException {
		this.path = path;
		file = new File(path);
		if(!file.exists())
			throw new java.io.FileNotFoundException(path);
		randomFile = new RandomAccessFile(file, "rw");
	}

	/**
	 * 
	 * @param file 文件
	 * @param createIfNotExist 当文件不存在的时候，是否建立新文件
	 * @throws FileNotFoundException 
	 */
	public Texter(File file, boolean createIfNotExist){
		this.path = file.getAbsolutePath();
		this.file = file;
		if (createIfNotExist)
			if (!file.exists()) {
				try {
					file.createNewFile();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		try {
			randomFile = new RandomAccessFile(file, "rw");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 相当于TextOperator(file，false)
	 * @param file 文件
	 * @throws FileNotFoundException 
	 */
	public Texter(File file) throws FileNotFoundException {
		this.path = file.getAbsolutePath();
		this.file = file;
		if(!file.exists())
			throw new java.io.FileNotFoundException(path);
		randomFile = new RandomAccessFile(file, "rw");
	}
	
	/**
	 * 将文本文件一行一行读出,放到一个List中
	 * @return List<String>
	 * @see java.io.BufferedReader#readLine()
	 */
	public List<String> readLines() {
		InputStreamReader fr;
		List<String> list = new ArrayList<String>();
		try {
			fr = new InputStreamReader(new FileInputStream(file), fileEncoding);
			BufferedReader br = new BufferedReader(fr);
			String line = br.readLine();
			while (line != null) {
				list.add(line);
				line = br.readLine();
			}
			br.close();
			fr.close();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return list;
	}

	/**
	 * 将文件全部读出
	 * @return String
	 * @throws IOException 
	 */
	public String readAll() throws IOException {
		InputStreamReader fr;
		StringBuffer sb = new StringBuffer();
		char[] buffer=new char[1024];
		fr = new InputStreamReader(new FileInputStream(file), fileEncoding);
		int len=0;
		while((len=fr.read(buffer))>0){
			sb.append(buffer, 0, len);
		}
		fr.close();
		return sb.toString();
	}

	/**
	 * 在文件末尾添加
	 * 
	 * @param content
	 */
	public void append(String content) {
		try {
			long fileLength = randomFile.length();
			randomFile.seek(fileLength);
			randomFile.write(content.getBytes(fileEncoding));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 在文件末尾添加
	 * 
	 * @param content
	 */
	public void appendLine(String content,String lineEnd) {
		try {
			long fileLength = randomFile.length();
			randomFile.seek(fileLength);
			randomFile.write(content.getBytes(fileEncoding));
			randomFile.write(lineEnd.getBytes());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 在文件末尾添加
	 * 
	 * @param content
	 */
	public void appendLine(String content) {
		appendLine(content,"\r\n"); 
	}
	
	/**
	 * 覆写文件
	 * 
	 * @param content
	 */
	public void overWrite(String content) {
		try {
			randomFile.setLength(0);
			randomFile.write(content.getBytes(fileEncoding));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 关闭
	 */
	public void close() {
		try {
			if (randomFile != null)
				randomFile.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取文件编码,事实上这并不是文件真正的编码,只是规定了用什么编码去读写文件
	 * 
	 * @return FileEncoding
	 */
	public String getFileEncoding() {
		return fileEncoding;
	}

	/**
	 * 设置读写文件的编码
	 * 
	 * @param fileEncoding
	 * @return this
	 */
	public Texter setFileEncoding(String fileEncoding) {
		this.fileEncoding = fileEncoding;
		return this;
	}

	/**
	 * 获取文件引用
	 * 
	 * @return this.file
	 */
	public File getFile() {
		return this.file;
	}

	/**
	 * 将文件全部读出
	 * @throws IOException 
	 */
	public static String readText(String path,String fileEncoding) throws IOException {
		File file=new File(path);
		InputStreamReader fr;
		StringBuffer sb = new StringBuffer();
		char[] buffer=new char[1024];
		fr = new InputStreamReader(new FileInputStream(file), fileEncoding);
		int len=0;
		while((len=fr.read(buffer))>0){
			sb.append(buffer, 0, len);
		}
		fr.close();
		return sb.toString();
	}
}